package protectjava.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo1 {
//    通过jdbc来控制事物
    public static void main(String[] args) {
        Connection conn = null;
        try {
//            1.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
//            2.获得connrction
             conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/runoob?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC", "root", "123");

//            手动开启事物
            conn.setAutoCommit(false);

//            3.获得执行平台
            Statement stmt = conn.createStatement();
//            4.操作sql
            stmt.executeUpdate("insert into account values (null ,'小明',3000)");
//              提交事物
            conn.commit();

            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }

        }
    }
}
